import { defineStore } from 'pinia'
import config from '@/config'

export const useAppStore = defineStore('app', {
  state: () => ({
    // 应用标题
    title: config.app.title,
    // 侧边栏状态
    sidebarCollapsed: false,
    // 主题模式
    theme: localStorage.getItem('theme') || 'light',
    // 语言设置
    language: localStorage.getItem('language') || 'zh-CN',
    // 加载状态
    loading: false,
    // 全局消息
    message: {
      type: '', // success, error, warning, info
      content: '',
      visible: false
    },
    // 移动端菜单状态
    mobileMenuOpen: false,
    // 页面标题
    pageTitle: ''
  }),

  getters: {
    // 获取完整标题
    fullTitle: (state) => {
      return state.pageTitle ? `${state.pageTitle} - ${state.title}` : state.title
    },

    // 是否为暗色主题
    isDarkTheme: (state) => state.theme === 'dark',

    // 是否为移动端
    isMobile: () => window.innerWidth <= 768,

    // 是否显示侧边栏
    showSidebar: (state) => !state.sidebarCollapsed && !state.isMobile
  },

  actions: {
    // 设置页面标题
    setPageTitle(title) {
      this.pageTitle = title
      document.title = this.fullTitle
    },

    // 切换侧边栏
    toggleSidebar() {
      this.sidebarCollapsed = !this.sidebarCollapsed
    },

    // 设置侧边栏状态
    setSidebarCollapsed(collapsed) {
      this.sidebarCollapsed = collapsed
    },

    // 切换主题
    toggleTheme() {
      this.theme = this.theme === 'light' ? 'dark' : 'light'
      localStorage.setItem('theme', this.theme)
      this.applyTheme()
    },

    // 设置主题
    setTheme(theme) {
      this.theme = theme
      localStorage.setItem('theme', theme)
      this.applyTheme()
    },

    // 应用主题
    applyTheme() {
      const html = document.documentElement
      if (this.theme === 'dark') {
        html.classList.add('dark')
      } else {
        html.classList.remove('dark')
      }
    },

    // 设置语言
    setLanguage(language) {
      this.language = language
      localStorage.setItem('language', language)
    },

    // 设置加载状态
    setLoading(loading) {
      this.loading = loading
    },

    // 显示消息
    showMessage(type, content, duration = 3000) {
      this.message = {
        type,
        content,
        visible: true
      }

      if (duration > 0) {
        setTimeout(() => {
          this.hideMessage()
        }, duration)
      }
    },

    // 隐藏消息
    hideMessage() {
      this.message.visible = false
    },

    // 显示成功消息
    showSuccess(content, duration) {
      this.showMessage('success', content, duration)
    },

    // 显示错误消息
    showError(content, duration) {
      this.showMessage('error', content, duration)
    },

    // 显示警告消息
    showWarning(content, duration) {
      this.showMessage('warning', content, duration)
    },

    // 显示信息消息
    showInfo(content, duration) {
      this.showMessage('info', content, duration)
    },

    // 切换移动端菜单
    toggleMobileMenu() {
      this.mobileMenuOpen = !this.mobileMenuOpen
    },

    // 关闭移动端菜单
    closeMobileMenu() {
      this.mobileMenuOpen = false
    },

    // 初始化应用
    initApp() {
      // 应用主题
      this.applyTheme()

      // 设置初始页面标题
      document.title = this.fullTitle

      // 监听窗口大小变化
      window.addEventListener('resize', () => {
        if (this.isMobile && this.mobileMenuOpen) {
          this.closeMobileMenu()
        }
      })
    }
  }
})
